/*
* @Authoer: weimei
* @Description: 菜单配置
* @Date 2017/12/10 9：30deleteFn
* @Modified By:
*/
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/content/systemManager/menuConfig/menuConfig.js',
  './custom/widgets/js/yufpExtTree.js'
], function (require, exports) {
  exports.ready = function (hashCode, data, cite) {
    yufp.custom.vue({
      el: cite.el,
      data: function () {
        var me = this;
        // 排序校验
        var orderValidate = function (rule, value, callback) {
          var reg = /^\d{0,4}$/;
          if (!reg.test(value)) {
            callback(new Error(me.$t('menuConfig.qsrzs')));
            return;
          }
          callback();
        };
        return {
          funcFormdata: {},
          height: yufp.custom.viewSize().height,
          currClickNode: '',
          currClickName: '',
          addFlag: false,
          filterNode: '',
          tempCheckNode: '',
          iconDialogVisible: false,
          createCheck: !yufp.session.checkCtrl('add'), // 新增按钮控制
          deleteCheck: !yufp.session.checkCtrl('delete'), // 删除按钮控制
          menuTreeUrl: backend.appOcaService + '/api/adminsmmenu/menutreequery',
          dataParams: {sysId: yufp.session.logicSys.id, lazy: true},
          funcUrl: backend.appOcaService + '/api/adminsmmenu/funclistquery',
          expandCollapseName: ['funcList'],
          icons: yufp.util.icons(),
          tableColumns: [
            { label: this.$t('menuConfig.mkmc'), prop: 'modName', width: 145, sortable: true, resizable: true },
            { label: this.$t('menuConfig.ywgnmc'), prop: 'funcName', width: 150, sortable: true, resizable: true },
            { label: this.$t('menuConfig.lj'), prop: 'funcUrl', sortable: true, resizable: true, showOverflowTooltip: true }
          ],
          menuFields: [
            {
              columnCount: 1,
              fields: [
                {
                  field: 'menuName',
                  label: this.$t('menuConfig.cdmc'),
                  rules: [
                    { required: true, message: this.$t('menuConfig.btx'), trigger: 'blur' },
                    { max: 23, message: this.$t('menuConfig.srzgc'), trigger: 'blur' },
                    { validator: yufp.validator.speChar, message: this.$t('menuConfig.srxxbhtszf'), trigger: 'blur' }
                  ]
                },
                {
                  field: 'funcName',
                  label: this.$t('menuConfig.ywgn'),
                  placeholder: this.$t('menuConfig.qcywgnlbxz'),
                  readonly: true,
                  focus: function (event) {
                    me.expandCollapseName = [];
                    me.expandCollapseName.push('funcList');
                  },
                  icon: 'circle-close',
                  click: function () {
                    me.$refs.menuForm.formModel.funcId = '';
                    me.$refs.menuForm.formModel.funcName = '';
                  }
                },
                {
                  field: 'menuOrder',
                  label: this.$t('menuConfig.px'),
                  rules: [
                    { validator: orderValidate, trigger: 'blur' }
                  ]
                },
                {
                  field: 'menuIcon',
                  label: this.$t('menuConfig.tb'),
                  readonly: true,
                  focus: function (event) {
                    me.iconDialogVisible = true;
                  },
                  icon: 'circle-close',
                  click: function () {
                    me.$refs.menuForm.formModel.menuIcon = '';
                  }
                },
                {
                  field: 'upMenuName',
                  label: this.$t('menuConfig.sjjd'),
                  readonly: true,
                  value: this.$t('menuConfig.zcd'),
                  icon: 'circle-close',
                  focus: function (event) {
                    me.expandCollapseName = [];
                    me.expandCollapseName.push('upMenu');
                    me.$refs.menuForm.formModel.upMenuId = '0';
                    me.$refs.menuForm.formModel.upMenuName = this.$t('menuConfig.zcd');
                    me.currClickNode = '0';
                    this.currClickName = this.$t('menuConfig.zcd');
                  },
                  rules: [{ required: true, message: this.$t('menuConfig.btx'), trigger: 'blur' }]
                },
                {
                  field: 'menuTip',
                  label: this.$t('menuConfig.sm'),
                  type: 'textarea',
                  rules: [{ max: 23, message: this.$t('menuConfig.srzgc'), trigger: 'blur' }]
                }
              ]
            }
          ],
          formButtons: [
            {
              label: this.$t('menuConfig.bc'),
              type: 'primary',
              hidden: false,
              op: 'submit',
              click: function (model, valid) {
                if (valid) {
                  model.lastChgUsr = yufp.session.userId;
                  if (me.addFlag || model.menuId == undefined) { // 新增
                    model.sysId = yufp.session.logicSys.id;
                    model.upMenuId = me.currClickNode || '0';
                    yufp.service.request({
                      method: 'POST',
                      url: backend.appOcaService + '/api/adminsmmenu/createmenu',
                      data: model,
                      callback: function (code, message, response) {
                        if (code == '0' && response.code == 0) {
                          me.dialogVisible = false;
                          me.$message({ message: me.$t('menuConfig.sjbccg') });
                          me.$refs.menuTree.refresh();
                          me.$refs.upMenuTree.refresh();
                          me.$refs.menuForm.resetFields();
                          me.$refs.menuForm.formModel.funcId = '';
                          delete me.$refs.menuForm.formModel.menuId;
                        }
                      }
                    });
                    me.addFlag = false;
                  } else { // 修改
                    yufp.service.request({
                      method: 'POST',
                      url: backend.appOcaService + '/api/adminsmmenu/editmenu',
                      data: model,
                      callback: function (code, message, response) {
                        if (code == '0' && response.code == 0) {
                          me.dialogVisible = false;
                          me.$message({ message: me.$t('menuConfig.sjbccg') });
                          me.$refs.menuTree.refresh();
                          me.$refs.upMenuTree.refresh();
                          me.$refs.menuForm.resetFields();
                          me.$refs.menuForm.formModel.funcId = '';
                          delete me.$refs.menuForm.formModel.menuId;
                        }
                      }
                    });
                  }
                } else {
                  me.$message({ message: me.$t('menuConfig.qjcsrxsfhf'), type: 'warning' });
                  return false;
                }
              }
            },
            {
              label: this.$t('menuConfig.zz'),
              hidden: false,
              click: function (model) {
                me.$nextTick(function () {
                  me.$refs.menuForm.formModel.upMenuId = '';
                  me.$refs.menuForm.formModel.funcId = '';
                  me.$refs.menuForm.resetFields();
                });
              }
            }
          ]
        };
      },
      watch: {
        filterNode: function (val) {
          this.tempCheckNode = ',' + val + ',';
          this.$refs.upMenuTree.filter(val);
        }
      },
      methods: {
        // 菜单树点击事件
        nodeClickFn: function (nodeData, node, self) {
          this.currClickNode = nodeData.id;
          this.currClickName = nodeData.label;
          this.filterNode = nodeData.id;
          this.addFlag = false;
          var param = {
            'menuId': nodeData.id
          };
          var _this = this;
          yufp.service.request({
            method: 'GET',
            data: param,
            url: backend.appOcaService + '/api/adminsmmenu/menuinfoquery',
            callback: function (code, message, response) {
              if (code == '0' && response.code == 0) {
                var formModel = yufp.extend({}, response.data);
                if (nodeData.pid == '0') { // 当选择的是以及菜单的时候，上级节点名称就直接赋-值主菜单--hujun3 2020-01-13
                  // formModel.upMenuName = _this.$refs.menuTree.data[0].label;//
                  formModel.upMenuName = _this.$t('menuConfig.zcd');
                }
                _this.$refs.menuForm.formModel = formModel;
              }
            }
          });
        },
        // 右侧菜单树节点过滤：修改时菜单自身节点及子节点不能作为其上层菜单,因此过滤不展示
        filterFn: function (value, data) {
          var me = this;
          if (me.tempCheckNode.indexOf(',' + data.id + ',') >= 0 || me.tempCheckNode.indexOf(',' + data.pid + ',') >= 0) {
            me.tempCheckNode += data.id + ',';
            return false;
          } else {
            return true;
          }
        },
        // 右侧菜单树，点击选择上层菜单
        upMenuClickFn: function (nodeData, node, self) {
          this.currClickNode = nodeData.id;
          this.currClickName = nodeData.label;
          var formModel = yufp.extend({}, this.$refs.menuForm.formModel);
          formModel.upMenuId = nodeData.id;
          formModel.upMenuName = nodeData.label;
          this.$refs.menuForm.formModel = yufp.extend({}, formModel);
        },
        // 点击新增按钮后的响应事件
        createFn: function () {
          var me = this;
          if (me.currClickNode == '') {
            me.$message({ message: this.$t('menuConfig.qxxzcdjd'), type: 'warning' });
            return;
          }
          me.addFlag = true;
          var temp = {
            menuName: '',
            funcName: '',
            menuOrder: '',
            menuIcon: '',
            upMenuName: me.currClickName,
            menuTip: ''
          };
          me.$refs.menuForm.formModel = yufp.extend({}, temp);
          me.$refs.menuForm.formModel.funcId = '';
          delete me.$refs.menuForm.formModel.menuId;
        },
        // 业务功能列表选择
        funcSelect: function (row) {
          var formModel = yufp.extend({}, this.$refs.menuForm.formModel);
          formModel.funcId = row.funcId;
          formModel.funcName = row.funcName;
          this.$refs.menuForm.formModel = yufp.extend({}, formModel);
        },
        // 删除菜单
        deleteFn: function () {
          if (this.currClickNode == '') {
            this.$message({ message: this.$t('menuConfig.qxxzcdjd'), type: 'warning' });
            return;
          }
          var menuId = this.currClickNode;
          var _this = this;
          _this.$confirm(_this.$t('menuConfig.scgcdxdtsjscqzcdqdsc'), _this.$t('menuConfig.ts'), {
            confirmButtonText: _this.$t('menuConfig.qd'),
            cancelButtonText: _this.$t('menuConfig.qx'),
            type: 'warning'
          }).then(function () {
            yufp.service.request({
              method: 'POST',
              url: backend.appOcaService + '/api/adminsmmenu/deletemenu',
              data: {
                menuId: menuId
              },
              callback: function (code, message, response) {
                if (code == '0' && response.code == 0) {
                  _this.$message({ message: _this.$t('menuConfig.sccg') });
                  var param = {};
                  // 刷新树
                  _this.$refs.menuTree.refresh(param);
                  _this.$refs.upMenuTree.refresh(param);
                  _this.currClickNode = '';
                  _this.$refs.menuForm.resetFields();
                }
              }
            });
          });
        },
        // 图标点击事件
        handleIconClick: function () {
          this.iconDialogVisible = true;
        },
        // 获取图标
        iconSelect: function (event) {
          var iconName = event.target.className;
          var formModel = yufp.extend({}, this.$refs.menuForm.formModel);
          formModel.menuIcon = iconName;
          this.$refs.menuForm.formModel = yufp.extend({}, formModel);
          this.iconDialogVisible = false;
        }
      }
    });
  };
});
